﻿
using CommonCater;
using Lufei_CaterBLL;
using Lufei_CaterModel;
using Lufei_CaterUI;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace ItcastCater
{
    public partial class FrmAddMoney : Form
    {
        public FrmAddMoney()
        {
            InitializeComponent();
        }

        private void FrmAddMoney_Load(object sender, System.EventArgs e)
        {
            //加载DGV
            LoadProducts();
            //加载treeview
            LoadCategory();
            //加载点单信息
            LoadProdyctAndOrders(this.OrderID);
        }

        R_OrderInfo_Product_BLL r_OrderInfo_Product_ = new R_OrderInfo_Product_BLL();

        /// <summary>
        /// 加载点单信息
        /// </summary>
        /// <param name="orderID"></param>
        private void LoadProdyctAndOrders(int orderID)
        {
             dgvROrderProduct.AutoGenerateColumns = false;
             List<R_OrderInfo_Product> list = r_OrderInfo_Product_.GetOrderInfoProducts(orderID);
             dgvROrderProduct.DataSource= list;
            //算钱
            decimal sum = 0;
            for (int i = 0; i < list.Count; i++)
            {
                sum+= list[i].ProMoney;
            }

            labSumMoney.Text = sum.ToString();
            labCount.Text = list.Count.ToString();//数量赋值

            //解决思路 ：
            //1.使用窗体传值
            //2.从数据库再找一遍
            //3.静态变量  (全局共享)
            //R_OrderInfo_Product r = new R_OrderInfo_Product();
            //r.MONEY = sum;
            //r.CT = list.Count;

            //通过工具类的静态字段来存储总金额和总数量
            //MoneyAndCount.Money = sum;
            //MoneyAndCount.Count = list.Count;

        }

        OrderInfoBLL orderInfoBLL = new OrderInfoBLL();

        /// <summary>
        /// 窗体关闭调用
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmAddMoney_FormClosed(object sender, FormClosedEventArgs e)
        {
            //更新订单表中的金额数据 OrderID
            orderInfoBLL.UpdateMoney(this.OrderID,Convert.ToDecimal(labSumMoney.Text));
        }


        ProductBLL productBLL = new ProductBLL();

        /// <summary>
        /// 加载索引的商品并给产品赋值
        /// </summary>
        private void LoadProducts()
        {
            //获取索引的产品
            List<ProductInfo>  listProducts=   productBLL.GetProducts();
            dgvProduct.AutoGenerateColumns = false;//关闭自动生产列
            dgvProduct.DataSource= listProducts;
            dgvProduct.SelectedRows[0].Selected = false;//dgv的第一行不选中  这个和数据有关一定要在数据加载完成后设置
        }


        /// <summary>
        /// 获取所有的商品类型赋值给treeview
        /// </summary>
        private void LoadCategory()
        {
            CategoryBLL categoryBLL=new CategoryBLL();
            List<CategoryInfo> listCategory=  categoryBLL.GetCategoryInfos();
            //给Treebiew的根节点添加数据
            for (int i = 0; i < listCategory.Count; i++)
            {
              TreeNode tn =  tvCategory.Nodes.Add(listCategory[i].CatName);
              LoadCategoryByTreeNode(tn.Nodes, listCategory[i].CatId);
            }
        }


        /// <summary>
        /// 加载所有的子节点
        /// </summary>
        private void LoadCategoryByTreeNode(TreeNodeCollection tc,int id)
        {
            List<ProductInfo> listProduct =   productBLL.GetProductInfoByID(id);

            for (int i = 0; i < listProduct.Count; i++)
            {
                tc.Add(listProduct[i].ProName+"—"+ listProduct[i].ProPrice);
            }
        }

        //桌子id
        public int OrderID { get; set; }

        public void SetValue(object sender,EventArgs e)
        {
            UpdateMemmberInfo<FrmBilling> update=e as UpdateMemmberInfo<FrmBilling>;
            if (update!=null)
            {
                labDeskName.Text = update.Name;
                //春初桌子id
                this.OrderID = update.Type;
            }
        }

        /// <summary>
        /// 搜索
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            string str=txtSearch.Text.Trim();
            if (string.IsNullOrEmpty(str))
            {
                //空的显示全部
                LoadCategory();
            }
            else
            {
                int temp = 0;
                //判断用户数据的是字符串还是数字
                if (char.IsLetter(str[0]))
                {
                    temp = 1;
                  dgvProduct.DataSource=  productBLL.GetProductInfoBySpellOrNum(str,temp);
                }
                else
                {
                    temp = 2;
                    dgvProduct.DataSource = productBLL.GetProductInfoBySpellOrNum(str, temp);
                }
            }
        }

        /// <summary>
        /// 双击点菜
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvProduct_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            //1.如果用户输入的是0或者1  或者什么都不输入  ，统一按照数量1处理

            //2.如果用户输入的不是正确的数字，提示用户  重新输入  isletter(只能判断字符) 如果用户输入符号判断不了 tryparse

            //3.正常
            int count = 0;//记录数量
            if (string.IsNullOrEmpty(txtCount.Text.Trim())||txtCount.Text == "0" ||txtCount.Text=="1")
            {
                count = 1;
            }
            else if (!int.TryParse(txtCount.Text,out count))
            {
                MessageBox.Show("请输入正确的数量!!!!");
                log<FrmAddMoney>.LogDebug("用户没有输入数字");
                txtCount.Clear();//清除文本框
                return;
            }
            else
            {
                count = Convert.ToInt32(txtCount.Text.Trim());
            }
            //插入数据
            //创建插入R_OrderInfo_Product实体对象
            R_OrderInfo_Product r = new R_OrderInfo_Product();
            r.OrderId = this.OrderID;
            r.ProId = Convert.ToInt32(dgvProduct.SelectedRows[0].Cells[0].Value);
            r.DelFlag = 0;
            r.SubTime = DateTime.Now;
            r.UnitCount = count;
            r.State = 0;
            r_OrderInfo_Product_.InsertDate(r);
            LoadProdyctAndOrders(r.OrderId);

        }

        /// <summary>
        /// 确定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOk_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 退菜
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDeleteRorderPro_Click(object sender, EventArgs e)
        {
            //选中的数据  退菜
            //拿到选中的数据   拿id
            int id = Convert.ToInt32(dgvROrderProduct.SelectedRows[0].Cells[0].Value);
            bool isDeleted= r_OrderInfo_Product_.DelteInfoByID(id);
            if (isDeleted)
            {
                MessageBox.Show("删除成功!!!!");
                log<FrmAddMoney>.LogDebug("删除菜品成功");

                //刷新页面
                LoadProdyctAndOrders(this.OrderID);
            }
        }


    }
}
